package usedForPage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.NavigableSet;
import java.util.SortedMap;
import java.util.TreeMap;

public class Searcher {
	public static List<String> lstProviders;

	public static List<String> getLstName(String name, int howMany) {
		List<String> lstNames = new ArrayList<String>();
		List<NickNames> lstNick = Reader.getLstNickNames();
		try {
			name = name.toLowerCase();
			SortedMap<Double, String> smDistanceName = new TreeMap<Double, String>();
			double totalMin;
			List<String> lstLookingName = new ArrayList<String>();
			lstLookingName.add(name);
			HashMap<String, Double> hmNameProb = new HashMap<String, Double>();
			hmNameProb.put(name, 1.0);
			String t3;
			String[] tem = name.split(" ");
			for (String t1 : tem) {
				for (NickNames nic : lstNick) {
					if (nic.getNickname().equalsIgnoreCase(t1)) {
						t3 = name.replaceAll(t1, nic.getRealName());
						lstLookingName.add(t3);
						hmNameProb.put(t3, (2 - nic.getProb()));
					}
				}
			}
			HashMap<String, Double> added = new HashMap<String, Double>();
			for (String searchedName : lstLookingName) {
				for (int i = 0; i < lstProviders.size(); i++) {
					totalMin = Distance.getDistanceForProviders(searchedName, lstProviders.get(i));
					if(lstProviders.get(i).equalsIgnoreCase("Peter C Meyer MD"))
						System.out.println("total = " + totalMin);
					totalMin = totalMin * hmNameProb.get(searchedName);
					while (smDistanceName.containsKey(totalMin)) {
						totalMin = totalMin - 0.0001;
					}
					if (added.containsKey(lstProviders.get(i))) {
						if (totalMin < added.get(lstProviders.get(i))) {
							smDistanceName.remove(added.get(lstProviders.get(i)));
							smDistanceName.put(totalMin, lstProviders.get(i));
							added.put(lstProviders.get(i), totalMin);
						}
					} else {
						added.put(lstProviders.get(i), totalMin);
						smDistanceName.put(totalMin, lstProviders.get(i));
					}
				}
			}
			NavigableSet<Double> navigScoreName = ((TreeMap<Double, String>) smDistanceName).navigableKeySet();
			for (double sortedDiss : navigScoreName) {
				lstNames.add(smDistanceName.get(sortedDiss));
				if (lstNames.size() >= howMany)
					break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lstNames;
	}
}
